<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ID" prop="institutionId">
        <el-input v-model="queryParams.institutionId" placeholder="请输入ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="机构名称" prop="institutionName">
        <el-input v-model="queryParams.institutionName" placeholder="请输入机构名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['book:institutions:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['book:institutions:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['book:institutions:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['book:institutions:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="institutionsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="institutionId" />
      <el-table-column :show-overflow-tooltip="true" label="机构" align="center" prop="institutionName" />
      <el-table-column :show-overflow-tooltip="true" label="host" align="center" prop="hostUrl" />
      <el-table-column :show-overflow-tooltip="true" label="jwt_secret" align="center" prop="jwtSecret" />
      <el-table-column :show-overflow-tooltip="true" label="app_id" align="center" prop="appId" />
      <el-table-column :show-overflow-tooltip="true" label="app_key" align="center" prop="appKey" />
      <el-table-column :show-overflow-tooltip="true" label="备用字段01" align="center" prop="backupField01" />
      <el-table-column :show-overflow-tooltip="true" label="备用字段02" align="center" prop="backupField02" />
      <el-table-column :show-overflow-tooltip="true" label="备用字段03" align="center" prop="backupField03" />
      <el-table-column :show-overflow-tooltip="true" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['book:institutions:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['book:institutions:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改机构信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="机构" prop="institutionName">
          <el-input v-model="form.institutionName" placeholder="请输入机构名称" />
        </el-form-item>
        <el-form-item label="host地址" prop="hostUrl">
          <el-input v-model="form.hostUrl" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="jwt_secret" prop="jwtSecret">
          <el-input v-model="form.jwtSecret" placeholder="请输入jwt_secret" />
        </el-form-item>
        <el-form-item label="app_id" prop="appId">
          <el-input v-model="form.appId" placeholder="请输入app_id" />
        </el-form-item>
        <el-form-item label="app_key" prop="appKey">
          <el-input v-model="form.appKey" placeholder="请输入app_key" />
        </el-form-item>
        <el-form-item label="备用字段01" prop="backupField01">
          <el-input v-model="form.backupField01" placeholder="请输入备用字段01" />
        </el-form-item>
        <el-form-item label="备用字段02" prop="backupField02">
          <el-input v-model="form.backupField02" placeholder="请输入备用字段02" />
        </el-form-item>
        <el-form-item label="备用字段03" prop="backupField03">
          <el-input v-model="form.backupField03" placeholder="请输入备用字段03" />
        </el-form-item>
        <el-divider content-position="center">图书委托用户信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini"
              @click="handleAddBookCommissionUsers">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini"
              @click="handleDeleteBookCommissionUsers">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="bookCommissionUsersList" :row-class-name="rowBookCommissionUsersIndex"
          @selection-change="handleBookCommissionUsersSelectionChange" ref="bookCommissionUsers">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="密码" prop="userPassword" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.userPassword" placeholder="请输入密码" />
            </template>
          </el-table-column>
          <el-table-column label="账号类型" prop="userType" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.userType" placeholder="请选择账号类型，0，1等">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="用户名" prop="userName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.userName" placeholder="请输入用户名" />
            </template>
          </el-table-column>
          <el-table-column label="电话" prop="userPhone" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.userPhone" placeholder="请输入电话" />
            </template>
          </el-table-column>
          <el-table-column label="头像" prop="userAvatarUrl" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.userAvatarUrl" placeholder="请输入头像" />
            </template>
          </el-table-column>
          <el-table-column label="性别" prop="gender" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.gender" placeholder="请输入性别" />
            </template>
          </el-table-column>
          <el-table-column label="身份证" prop="idCard" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.idCard" placeholder="请输入身份证号" />
            </template>
          </el-table-column>
          <el-table-column label="学号" prop="studentId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.studentId" placeholder="请输入学号" />
            </template>
          </el-table-column>
          <el-table-column label="微信号" prop="wechatId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.wechatId" placeholder="请输入微信号" />
            </template>
          </el-table-column>
          <el-table-column label="银行类型" prop="bankType" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.bankType" placeholder="请选择银行类型">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="银行卡号" prop="bankCardNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.bankCardNumber" placeholder="请输入银行卡号" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInstitutions, getInstitutions, delInstitutions, addInstitutions, updateInstitutions } from "@/api/book/institutions";

export default {
  name: "Institutions",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBookCommissionUsers: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 机构信息表格数据
      institutionsList: [],
      // 图书委托用户表格数据
      bookCommissionUsersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        institutionId: null,
        institutionName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        institutionName: [
          { required: true, message: "机构名称不能为空", trigger: "blur" }
        ],
        hostUrl: [
          { required: true, message: "host地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询机构信息列表 */
    getList() {
      this.loading = true;
      listInstitutions(this.queryParams).then(response => {
        this.institutionsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        institutionId: null,
        institutionName: null,
        hostUrl: null,
        jwtSecret: null,
        appId: null,
        appKey: null,
        backupField01: null,
        backupField02: null,
        backupField03: null
      };
      this.bookCommissionUsersList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.institutionId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加机构信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const institutionId = row.institutionId || this.ids
      getInstitutions(institutionId).then(response => {
        this.form = response.data;
        this.bookCommissionUsersList = response.data.bookCommissionUsersList;
        this.open = true;
        this.title = "修改机构信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.bookCommissionUsersList = this.bookCommissionUsersList;
          if (this.form.institutionId != null) {
            updateInstitutions(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInstitutions(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const institutionIds = row.institutionId || this.ids;
      this.$modal.confirm('是否确认删除机构信息编号为"' + institutionIds + '"的数据项？').then(function () {
        return delInstitutions(institutionIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 图书委托用户序号 */
    rowBookCommissionUsersIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 图书委托用户添加按钮操作 */
    handleAddBookCommissionUsers() {
      let obj = {};
      obj.userPassword = "";
      obj.userType = "";
      obj.userName = "";
      obj.userPhone = "";
      obj.userAvatarUrl = "";
      obj.gender = "";
      obj.idCard = "";
      obj.studentId = "";
      obj.wechatId = "";
      obj.bankType = "";
      obj.bankCardNumber = "";
      this.bookCommissionUsersList.push(obj);
    },
    /** 图书委托用户删除按钮操作 */
    handleDeleteBookCommissionUsers() {
      if (this.checkedBookCommissionUsers.length == 0) {
        this.$modal.msgError("请先选择要删除的图书委托用户数据");
      } else {
        const bookCommissionUsersList = this.bookCommissionUsersList;
        const checkedBookCommissionUsers = this.checkedBookCommissionUsers;
        this.bookCommissionUsersList = bookCommissionUsersList.filter(function (item) {
          return checkedBookCommissionUsers.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleBookCommissionUsersSelectionChange(selection) {
      this.checkedBookCommissionUsers = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('book/institutions/export', {
        ...this.queryParams
      }, `institutions_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
